package xm.view
{
	import flash.desktop.NativeApplication;
	import flash.display.NativeWindow;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	
	import xm.components.SkinnableContainer;
	import xm.control.Navigator;
	import xm.core.XprimeMobileGlobals;
	import xm.debug.DEBUG_CollectDisObject;
	import xm.debug.DEBUG_MemoryUse;
	import xm.events.XMStateEvent;
	import xm.manager.LayoutMananger;
	import xm.manager.StageManager;
	import xm.namespaces.xprimeMobile;
	
	//-----------------------------------------------------------------------------------
	// namespace
	//-----------------------------------------------------------------------------------
	use namespace xprimeMobile;
	
	
	/**
	 * 어플리케이션 준비완료. 이시점부터 stage 참조 등 모든것이 제대로 동작한다.
	 */	
	[Event(name="stateApplicationComplete", type="xm.events.XMStateEvent")]
	
	/**
	 * <br> 클래스 설명 : 모바일 어플리케이션 메인</br>
	 * @author 김정환
	 */	
	public class ViewNavigatorApplication extends SkinnableContainer
	{
		
		//-----------------------------------------------------------------------------------
		// public vars
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// protected / internal vars
		//-----------------------------------------------------------------------------------
		
		
		
		
		
		//-----------------------------------------------------------------------------------
		// private vars
		//-----------------------------------------------------------------------------------
		private var _firstView : Class;
		private var _debuggerCDO : DEBUG_CollectDisObject;
		private var _debuggerMU : DEBUG_MemoryUse
		private var _isAppComRecieved : Boolean = false;
		private var _isDispatchApplicationComplete : Boolean = false;
		
		
		//-----------------------------------------------------------------------------------
		// public functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * constructor 
		 */	
		public function ViewNavigatorApplication()
		{
			if( XprimeMobileGlobals.layoutManager == null ) 
			{
				XprimeMobileGlobals.layoutManager = new LayoutMananger( this );
			}
			super();
		}
		
		
		/**
		 * commitProperties
		 */		
		override public function commitProperties():void
		{
			super.commitProperties();
			Navigator.setInstance( this );
		}
		
		
		
		/**
		 * updateDisplayList
		 */		
		override public function updateDisplayList( enforce : Boolean = false ):void
		{
			super.updateDisplayList();
		}
		
		
		
		
		
		
		//-----------------------------------------------------------------------------------
		// life cycle functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * createChildren
		 */		
		override public function createChildren():void
		{
			super.createChildren();
			
		}
		
		
		/**
		 * createComplete
		 */		
		override public function creationComplete():void
		{
			super.creationComplete();
			
		}
		
		
		/**
		 * add
		 */		
		override public function add():void
		{
			super.add();
			
			StageManager.getInstance().setStage( this );
			stage.addEventListener(Event.RESIZE, onStageResize );
			
			// 디버그모드.
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown );
			
			Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
			NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, onCom );
		}
		
		
		/**
		 * Application의 셋팅이 최종완료되었을때. 
		 */		
		public function applicationComplete():void
		{
			var xmStateEvent : XMStateEvent = new XMStateEvent( XMStateEvent.STATE_APPLICATION_COMPLETE, false, false );
			dispatchEvent( xmStateEvent );
			
			
			_isDispatchApplicationComplete = true;
			
		}
		
		/**
		 * <br>디버깅모드 사용.</br>
		 * <br> CDO- 키보드의 좌,우,상,하 화살표로 디버깅 가능 </br> 
		 * <br> CDO- 좌 : 마우스이벤트 중지 </br> 
		 * <br> CDO- 우 : 마우스이벤트 활성화 </br> 
		 * <br> CDO- 상 : 부모를 찾아 표시 </br> 
		 * <br> CDO- 하 : 디버깅 일시중지 ( 상 버튼으로 일시중지 해제 )</br> 
		 */		
		public function debugON( isCDO : Boolean = true, isMU : Boolean = true ):void
		{
			if( _debuggerCDO == null ) _debuggerCDO = new DEBUG_CollectDisObject( this.stage );
			if( _debuggerMU == null ) _debuggerMU = new DEBUG_MemoryUse( this.stage );
			
			if( isCDO ) _debuggerCDO.ON();
			if( isMU ) _debuggerMU.ON();
		}
		
		/**
		 * 디버깅 사용 해제
		 */		
		public function debugOFF():void
		{
			if( _debuggerCDO == null ) _debuggerCDO = new DEBUG_CollectDisObject( this.stage );
			if( _debuggerMU == null ) _debuggerMU = new DEBUG_MemoryUse( this.stage );
			_debuggerCDO.OFF();
			_debuggerMU.OFF();
		}
		
		//-----------------------------------------------------------------------------------
		// setter / getter
		//-----------------------------------------------------------------------------------
		
		/**
		 * @return 첫번째 view - View 확장클래스 여야한다.
		 */		
		public function get firstView():Class
		{
			return _firstView;
		}
		
		/**
		 * @param value 첫번째 view 설정.
		 */		
		public function set firstView(value:Class):void
		{
			_firstView = value;
			navigator.add( _firstView, false );
		}
		
		//-----------------------------------------------------------------------------------
		// protected functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// internal functions
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// private functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// event handler
		//-----------------------------------------------------------------------------------
		
		
		/**
		 * @param e 
		 */		
		protected function onStageResize(e:Event):void
		{
			trace(" ==> ViewNavigatorApplication ------------ " );
			
			invalidateDisplayList();
			
//			applicationComplete();
			
//			
//			if( _isDispatchApplicationComplete == false && stage.scaleMode != StageScaleMode.NO_SCALE && _isAppComRecieved )
//			{
//				applicationComplete();
//			}
		}
		
		
		/**
		 * @param e
		 */		
		protected function onCom(e:Event):void
		{
			trace(" ==> ViewNavigatorApplication onCom : -------------------" );
			
			e.currentTarget.removeEventListener( e.type, arguments.callee, e.bubbles );
			_isAppComRecieved = true;
			applicationComplete();
			
//			if( _isDispatchApplicationComplete == false && stage.scaleMode == StageScaleMode.NO_SCALE && _isAppComRecieved )
//			{
//				applicationComplete();
//			}
		}		
		
		
		
		
		
		
		
		////--------- debug
		
		private var cdo : Boolean = false;
		private var mu : Boolean = false;
		private function onKeyDown(e:KeyboardEvent):void
		{
			switch( e.keyCode )
			{
				case Keyboard.F1 :
					mu = true;
					break;
				
				case Keyboard.F2 :
					cdo = true;
					break;
			}
			
			debugON( mu, cdo );
		}
		
		
	}
}